package team.scau.laboratory_repair.web.pc;

import com.querydsl.core.QueryResults;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.common.json.JqGridResult;
import team.scau.laboratory_repair.common.json.PoToDto;
import team.scau.laboratory_repair.common.page.MyPage;
import team.scau.laboratory_repair.common.utils.JsonTool;
import team.scau.laboratory_repair.modules.dto.AreaDto;
import team.scau.laboratory_repair.modules.po.Area;
import team.scau.laboratory_repair.modules.po.QArea;
import team.scau.laboratory_repair.modules.po.User;
import team.scau.laboratory_repair.service.AreaService;
import team.scau.laboratory_repair.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description:
 * @Author:ZTF
 * @Date:2018/5/23 13:46
 */
@Controller
@CommonsLog
@RequestMapping(value="/pc/biz/area")
public class AreaController extends BaseController {

    @Autowired
    private AreaService areaService;

    @Autowired
    private UserService userService;

    @Autowired
    private PoToDto poToDto;

    @ModelAttribute("area")
    public Area get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return areaService.findById(id);
        } else {
            return new Area();
        }
    }

    @RequiresPermissions("biz:area:list")
    @GetMapping(value = "/list", produces = "text/html;charset=utf-8")
    public String list(Model model) throws Exception{
        return "/pc/biz/area/list";
    }

    @ResponseBody
    @RequiresPermissions("biz:area:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public JqGridResult list(@ModelAttribute Area area) throws Exception {
        MyPage page = getPage(QArea.class);
        QueryResults<Area> list = areaService.listOrByExample(area, page);
        List<AreaDto> areaDtos = poToDto.areaList(list.getResults());
        return new JqGridResult(areaDtos);
    }

    @RequiresPermissions("biz:area:edit")
    @GetMapping(value = "/edit", produces = "text/html;charset=utf-8")
    public String edit(@ModelAttribute("area") Area area, Model model) throws Exception{
        List<User> managers = userService.listAllManager();
        model.addAttribute("managers", managers);
        model.addAttribute("area",area);
        return "/pc/biz/area/edit";
    }

    @RequiresPermissions("biz:area:edit")
    @GetMapping(value = "/staffs", produces = "text/html;charset=utf-8")
    public String staffs(@ModelAttribute("area") Area area, Model model) throws Exception{
        model.addAttribute("area",area);
        model.addAttribute("staffTree", JsonTool.toJson(areaService.selectStaffTree(area)));
        return "/pc/biz/area/staffs";
    }

    @RequiresPermissions("biz:area:edit")
    @PostMapping(value = "/edit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Area area){
        Map<String,Object> result = new HashMap<>();
        try {
            areaService.save(area);
            result.put("status",200);
            result.put("msg","区域信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","区域信息保存失败");
        }
        return result;
    }
    @RequiresPermissions("biz:area:delete")
    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap<>();
        try {
            if(id != null && id > 0){
                areaService.deleteById(id);
                result.put("status",200);
                result.put("msg","删除区域成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的区域");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","删除区域失败");
        }
        return result;
    }
}
